const path = require('path');
const { logger } = require('./backend/src/utils/logger');

// 设置环境变量以使用模拟数据库
process.env.USE_MOCK_DB = 'true';

// 导入模拟数据库
const db = require('./backend/src/config/database');

async function debugMockDatabase() {
  try {
    console.log('\n=== 模拟数据库调试信息 ===\n');
    
    // 1. 检查菜单分类
    console.log('1. 菜单分类数据:');
    const categories = await db.query('SELECT * FROM menu_categories');
    console.log(`   总数: ${categories.length}`);
    categories.forEach(cat => {
      console.log(`   - ID: ${cat.id}, 名称: ${cat.name} (${cat.name_en})`);
    });
    
    // 2. 检查菜单项
    console.log('\n2. 菜单项数据:');
    const menuItems = await db.query('SELECT * FROM menu_items WHERE deleted_at IS NULL');
    console.log(`   总数: ${menuItems.length}`);
    menuItems.forEach(item => {
      console.log(`   - ID: ${item.id}, 名称: ${item.name} (${item.name_en}), 价格: $${item.price}, 分类ID: ${item.category_id}`);
      if (item.main_ingredients) {
        console.log(`     主要成分: ${item.main_ingredients}`);
      }
    });
    
    // 3. 检查导入任务
    console.log('\n3. 导入任务数据:');
    const importJobs = await db.query('SELECT * FROM import_jobs ORDER BY created_at DESC LIMIT 10');
    console.log(`   总数: ${importJobs.length}`);
    importJobs.forEach(job => {
      console.log(`   - 任务ID: ${job.job_id}, 状态: ${job.status}, 文件: ${job.file_name}`);
      console.log(`     总记录: ${job.total_records}, 成功: ${job.success_records}, 错误: ${job.error_records}`);
      console.log(`     创建时间: ${job.created_at}`);
    });
    
    // 4. 检查最近更新的菜单项
    console.log('\n4. 最近更新的菜单项:');
    const recentItems = await db.query('SELECT * FROM menu_items WHERE deleted_at IS NULL ORDER BY updated_at DESC LIMIT 5');
    recentItems.forEach(item => {
      console.log(`   - ${item.name}: 更新时间 ${item.updated_at}`);
    });
    
    // 5. 统计信息
    console.log('\n5. 统计信息:');
    console.log(`   - 活跃菜单分类: ${categories.filter(c => !c.deleted_at).length}`);
    console.log(`   - 活跃菜单项: ${menuItems.length}`);
    console.log(`   - 可用菜单项: ${menuItems.filter(i => i.is_available).length}`);
    console.log(`   - 导入任务: ${importJobs.length}`);
    
    console.log('\n=== 调试完成 ===\n');
    
  } catch (error) {
    console.error('调试模拟数据库时出错:', error);
  }
}

// 运行调试
debugMockDatabase();